package com.example.xunua_zhinengjiaotong.Dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.xunua_zhinengjiaotong.R;
import com.example.xunua_zhinengjiaotong.utils.HttpUtil;
import com.example.xunua_zhinengjiaotong.utils.MyApp;

import org.json.JSONObject;


public class ludeng3Dialog extends AlertDialog{
    private Context context;
    private Button btn_ok,btn_back;
    private RadioGroup rg;
    private RadioButton rb_manual, rb_auto;
    private MyApp myApp;
    private HttpUtil util=new HttpUtil();

    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    if ("ok".equals(msg.obj.toString())){
                        Toast.makeText(context, "设置成功", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(context, "设置失败", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    };

    public ludeng3Dialog(MyApp myApp,Context context) {
        super(context,R.style.myDialog);
        this.context=context;
        this.myApp=myApp;
    }

    protected ludeng3Dialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    protected ludeng3Dialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_dialog_ludeng3);
        initView();
    }

    private void initView() {
        btn_ok = findViewById(R.id.ludeng3_ok);
        btn_back=findViewById(R.id.ludeng3_back);
        rg=findViewById(R.id.ludeng3_rg);
        rb_manual =findViewById(R.id.ludeng3_rb_manual);
        rb_auto =findViewById(R.id.ludeng3_rb_auto);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (rg.getCheckedRadioButtonId()){
                    case R.id.ludeng3_rb_auto:
                        set("Auto");//设置模式自动
                        break;
                    case R.id.ludeng3_rb_manual:
                        set("Manual");//设置模式手动
                        break;
                        default:
                            Toast.makeText(context, "未选中", Toast.LENGTH_SHORT).show();
                            break;
                }
            }
        });
    }
    private void set(final String s){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject setRoadLightControlMode = util.connectServer("{\"ControlMode\":\"" + s + "\"}", myApp, "SetRoadLightControlMode");
                    String result = setRoadLightControlMode.getString("result");
                    Message message = new Message();
                    message.what=0;
                    message.obj=result;
                    handler.sendMessage(message);
                }catch (Exception e){

                }
            }
        }).start();
    }
    public interface Ludeng2Listener{
        void sendMessage(String s);
    }
}
